package wangjg.commons.page;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wangjg
 *
 */
public class WebPage extends Page{
    private long timeMillis = System.currentTimeMillis();
	private String[] queryParams ;//查询参数
	private String pagenoInput = "page.pageNo"; //页号参数名
    private String href = "";
    private String formid = "pageForm"+ timeMillis;
	public	String js_jump_function = "_wjg_jump_" + timeMillis;
    private HttpServletRequest request ;
    
    public WebPage() {
		super();
	}
    
	public WebPage(int pageSize, int pageno, int total) {
		super(pageSize, pageno, total);
	}
	
	public WebPage(int pageSize, int pageno) {
		super(pageSize, pageno);
	}
	
	public WebPage(int pageSize) {
		super(pageSize);
	}
	
	public String getPageNav(){
    	return null;
    }
	
    /**
     * 创建表单字符串
     * @return 表单字符串
     */
    public String getFormHtml(){
    	String inputFields = this.getInputFields();
    	String method = "get";
    	if(this.request!=null)
    		method = this.request.getMethod();
    	StringBuffer sb = new StringBuffer();
    	sb.append("\n<span style=\"display:none\">");
    	sb.append("\n<form id=\""+formid+"\" name=\""+formid+"\"");
    	sb.append(" method=\""+method.toLowerCase()+"\"");
    	sb.append(" action=\""+getAction()+"\"");
    	sb.append(">");
    	sb.append(inputFields);
    	sb.append("\n</form>");
    	sb.append("\n</span>");
    	return sb.toString();
    }
    /*
     * 创建 action字符串
     * @return
     */
    private String getAction(){
    	int pos=href.indexOf("?");
    	if(pos!=-1){
    		href=href.substring(0,pos);
    	}
    	return href;
    }
    private String getInputFields(){
    	if(request==null){
    		return null;
    	}
    	Map<String, String[]> params = request.getParameterMap();
    	StringBuffer sb = new StringBuffer();
    	Iterator<String> it = params.keySet().iterator();
    	boolean flag = false;
    	while(it.hasNext()){
    		String key = it.next().toString();
    		if(!checkParam(key))
    			continue;
    		String value = request.getParameter(key);
    		sb.append("\n<input type=\"hidden\" name=\"");
    		sb.append(key);
    		sb.append("\" value=\"");
    		sb.append(value);
    		sb.append("\" />");
    		if(key.equals(pagenoInput)){
    			flag = true;
    		}
    	}
    	if(!flag){
    		sb.append("\n<input type=\"hidden\" name=\"");
    		sb.append(pagenoInput);
    		sb.append("\" value=\"");
    		sb.append("\">");
    	}
    	return sb.toString();
    }
    
    private boolean checkParam(String param){
    	if(queryParams==null)
    		return true;//如果不设置查询参数，则都为true    	
    	for(int i=0;i<queryParams.length;i++){
    		if(queryParams[i].equals(param))
    			return true;
    	}
		return false;
    }
    //-----------------------------
    public String getPageLink(int pn){
    	String thref = this.getAction();

    	StringBuffer sb = new StringBuffer();
    	Map<String, String[]> params = request.getParameterMap();
    	Iterator<String> it = params.keySet().iterator();
    	while(it.hasNext()){
    		String key = it.next().toString();
    		if(!checkParam(key))
    			continue;
    		if(key.equals(pagenoInput)){
    			continue;
    		}
    		String value = request.getParameter(key);
    		if(value==null||value.isEmpty()){
    			continue;
    		}
    		sb.append(key);
    		sb.append("=");
    		sb.append(value);
    		sb.append('&');
    	}
    	sb.append(pagenoInput);
    	sb.append('=');
    	sb.append(pn);
    	if(sb.length()>0){
    		return thref+'?'+sb.toString();
    	}else{
    		return thref;
    	}
    	
    }
	/**
	 * 创建 客户端脚本
	 * @return
	 */
    public String getScript(){
		StringBuffer sb = new StringBuffer();
		sb.append("\n<script language=\"javascript\">");
		sb.append("\n	function "+js_jump_function+"(pageno,obj){");
		sb.append("\n		var current_pageno = "+this.getTruePageNo()+";");
		sb.append("\n		var current_pagecount = "+this.getPageCount()+";");
		sb.append("\n		if(!(/^\\d+$/.test(pageno))){return;}");
		sb.append("\n		var n = pageno*1;");
		sb.append("\n		if(n<1 || n==current_pageno || n>current_pagecount){");
		sb.append("\n			if(obj && obj.tagName=='A'){");
		sb.append("\n				obj.disabled=true; return;");
		sb.append("\n			}");
		sb.append("\n			return;");
		sb.append("\n		}");
		sb.append("\n		var form = document.getElementById('"+this.formid+"');");
		sb.append("\n		form['"+this.pagenoInput+"'].value=pageno;");
		sb.append("\n		form.submit();");
		sb.append("\n	}");
		sb.append("\n</script>");
		
		return sb.toString();
	}
    
    public String getJumpFun(int pageno){
    	return js_jump_function+"("+pageno+",this)";
    }
    
    public String getJumpFun(String s){
    	return js_jump_function+"("+s+")";
    }
	//-----------------------------
	
	/**
	 * @return 返回 formName。
	 */
	public String getFormid() {
		return formid;
	}
	/**
	 * @param formName 要设置的 formName。
	 */
	public void setFormid(String formid) {
		this.formid = formid;
	}
	/**
	 * @return 返回 href。
	 */
	public String getHref() {
		return href;
	}
	/**
	 * @param href 要设置的 href。
	 */
	public void setHref(String href) {
		this.href = href;
	}
	/**
	 * @return 返回 pageParam。
	 */
	public String getPagenoInput() {
		return pagenoInput;
	}
	/**
	 * @param pageParam 要设置的 pageParam。
	 */
	public void setPagenoInput(String pageParam) {
		this.pagenoInput = pageParam;
	}
	/**
	 * @return 返回 queryParams。
	 */
	public String[] getQueryParams() {
		return queryParams;
	}
	/**
	 * @param queryParams 要设置的 queryParams。
	 */
	public void setQueryParams(String[] queryParams) {
		this.queryParams = queryParams;
	}
	/**
	 * @return 返回 request。
	 */
	public HttpServletRequest getRequest() {
		return request;
	}
	/**
	 * @param request 要设置的 request。
	 */
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	/*-------------------------------------------------------------*/
	
}
